package com.nitty.cash.loop.ui.presenter

import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyConfigs
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.helper.NittyProcessHelper
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/19 11:59
 * @Annotation：进件 Presenter 实现
 */
class ProcessPresenter(private val iView: IViewContract.IProcessView): CommonPresenter(iView) {

    /**
     * 当前步骤的名称
     */
    private var eventStepTitle = ""
    fun getProcessEventTitle(): String{
        return eventStepTitle
    }

    /**
     * 根据Step类型显示不同的Page
     */
    fun loadProcessPageAction(){
        //移除所有步骤
        iView.removeProcessAllViews()

        val allStepTitles = NittyProcessHelper.getAuthProcessTitleList()
        //当前进行的步骤
        val curtIndex = NittyProcessHelper.getAuthProcessIndex()
        val allSteps = NittyProcessHelper.getAuthProcessAllStepList()
        for (i in 0 until allStepTitles.size) {
            val showStepTitle = allStepTitles[i]
            if (i < curtIndex){//已完成
                iView.showProcessAfterView(getProcessTypeDrawableByTitle(allSteps[i]),showStepTitle)
            } else if (i == curtIndex){//正在进行

            } else {//未开始
                iView.showProcessBeforeView(getProcessTypeDrawableByTitle(allSteps[i]),showStepTitle)
            }
        }

        if (curtIndex == allSteps.size){//所有步骤已完成
            iView.showProcessCompleteViews()
            return
        }
        val processData = NittyProcessHelper.getAuthProcessData()
        val showTitle = allStepTitles[curtIndex]
        eventStepTitle = allSteps[curtIndex]
        //是否是坦桑地区,需要根据语言显示
        if (NittyConfigs.isTanz()){
//            val stepTitleItem = JSONObject(stepTitle)
//            val curtLocale = Languages.getAppLanguage(iView.getAct())
//            stepTitle = when(curtLocale.language){
//                LocaleContract.getSwahiliLocale().language -> {//坦桑
//                    stepTitleItem.optString("sw")
//                }
//                else -> {
//                    stepTitleItem.optString("en")
//                }
//            }
        }
        //获取对应的key
        val curtKey = allSteps[curtIndex]
        //todo Contact 类型
        if (processData!!.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            stepData?.put("showTitle",showTitle)
            stepData?.put("stepTitle",eventStepTitle)
            stepData?.put("stepIcon",getProcessTypeDrawableByTitle(eventStepTitle))
            if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                iView.showProcessContactView(stepData)
                return
            }
        }

        //常规通用样式
        if (processData.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            if (stepData != null) {
                stepData.put("showTitle",showTitle)
                stepData.put("stepTitle",eventStepTitle)
                stepData.put("stepIcon",getProcessTypeDrawableByTitle(eventStepTitle))
                iView.showProcessCommonView(stepData)
            }
        }
    }

    /**
     * 提交数据
     */
    fun submitProcessAction(params: JSONObject){
        iView.showNittyLoading()
        val httpUrl = NittyApiUrl.getHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showProcessSubmitSuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    /**
     * 根据进件step名称获取对应的icon
     */
    private fun getProcessTypeDrawableByTitle(stepTitle: String): Int{
        when(stepTitle){
            "personal_info" -> {
                return R.mipmap.nitty_process_personal_info_icon
            }
            "work_info" -> {
                return R.mipmap.nitty_process_work_info_icon
            }
            "contact_info" -> {
                return R.mipmap.nitty_process_contact_info_icon
            }
            "account_info" -> {
                return R.mipmap.nitty_process_account_info_icon
            }
            "sefie_info" -> {
                return R.mipmap.nitty_process_sefie_info_icon
            }
            "ocr_info" -> {
                return R.mipmap.nitty_process_ocr_info_icon
            }
        }
        return R.mipmap.nitty_process_personal_info_icon
    }
}